<template>
  <div>
    <el-form ref="form" :inline="true" class="login-container" :model="form" :rules="rules">
        <h3 class="loging_title">系统登录</h3>
        <el-form-item prop="username" label="用户名">
            <el-input v-model="form.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password" label=" 密  码">
            <el-input type="password" v-model="form.password" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 130px; margin-top:10px;">
            <el-button @click="submit" type="primary">登录</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Mock from 'mockjs'
// 引入cookie，安装:npm i js-cookie@3.0.1
import Cookie from 'js-cookie'
import {getMenu} from '../api'
export default {
    data() {
        return {
            form:{
                username:'',
                password:''
            },
            rules:{
                username:[{required:true,trigger:'blur',message:'请输入用户名'}],
                password:[{required:true,trigger:'blur',message:'请输入密码'}]
            }
        }
    },
    methods: {
        // 登录
        submit(){
            // 调用Mock的随机数方法
            // const token = Mock.Random.guid()
            // 我们这里用cookie来存储 也可以用这个来存储:localStorage.setItem('token',token)

            // 校验
            this.$refs.form.validate((valid)=>{
                if (valid) {
                    getMenu(this.form).then(({data})=>{
                        console.log(data)
                        if (data.code===20000) {
                            // token信息存入cookie用于不同页面间的通信
                            Cookie.set('token',data.data.token)
                            // 获取菜单的数据，存入store中
                            this.$store.commit('tab/setMenu',data.data.menu)
                            this.$store.commit('tab/addMenu',this.$router)
                            this.$router.push({name:'home'})
                        }else{
                            this.$message({
                                showClose: true,
                                message:data.data.message,
                                type: 'error'
                            });
                        }
                    })
                }
            })
            
        }
    },
}
</script>

<style lang='less' scoped>
.login-container{
    width: 350px;
    border: 1px solid #eaeaea;
    margin: 180px auto;
    padding: 35px 25px 15px 55px;
    background-color: #fff;
    border-radius:15px ;
    box-shadow: 0 0 25px #cac6c6;
    .loging_title{
        text-align: center;
        margin-bottom:40px ;
        color: #505458;
    }

}
</style>